import express from 'express';
import axios from 'axios';
import cors from 'cors';

const app = express();

app.use(cors());

app.use('/', (req, res, next) => {
    if (req.url.startsWith('/api')) {
        next();
    } else {
        res.send('Welcome to the proxy server');
    }
});

app.use('/api', async (req, res) => {
    try {
        const url = 'https://news-at.zhihu.com' + req.url;
        const response = await axios({
            url,
            method: req.method,
            data: req.body,
            headers: req.headers
        });
        res.send(response.data);
    } catch (error) {
        res.status(error.response?.status || 500).send(error.message);
    }
});

app.listen(3000, () => {
    console.log('Proxy server is running on http://localhost:3000');
});
